Pacific Atlantic Water Flow

Try to solve the Pacific Atlantic Water Flow problem.

Statement#

Imagine an island with a rectangular shape that touches both the Pacific and Atlantic oceans. The northern and western sides meet the Pacific, while the southern and eastern sides touch the Atlantic. This island is divided into square cells.

To depict the height above sea level of each cell, we use an integer matrix, heights, of size (m×n)(m \times n). Here, heights[r][c] represents the elevation of the cell at coordinate (r,c)(r, c).

When heavy rain pours down on the island every few months, water flows from the island to both the Pacific and Atlantic oceans. The path of flow depends on the heights of the cells.

Consider a cell with a height of 99 that’s higher than or equal to its neighboring cells to the north, east, west, and south. This indicates that water can flow from this cell to adjacent ones. Similarly, this process is repeated until the flow of water either reaches the Pacific or Atlantic border or a cell that can not flow water to any of its neighbors. If the flow of water starting from a cell can direct water to both the Pacific and Atlantic Ocean borders, we include it in the output.

Note: Any cell adjacent to an ocean can channel water into the ocean.

With this information, your task is to return a 2D array of coordinates. Each entry (ri,ci)(r_i, c_i) in this array indicates that rainwater can flow from the cell at coordinate (ri,ci)(r_i, c_i) to both the Pacific and Atlantic oceans.

Constraints:

  • m==m == heights.length

  • n==n == heights[r].length, where 0≤r≤m0 \leq r \leq m

  • 1≤m1 \leq m, n≤200n \leq 200

  • 0≤0 \leq heights[r][c] ≤105\leq 10^5

Examples#

svg viewer

1 of 6

svg viewer

2 of 6

svg viewer

3 of 6

svg viewer

4 of 6

svg viewer

5 of 6

svg viewer

6 of 6

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

3

What is the output if the following array is given as input?

heights = [[13, 15, 0], [0, 4, 12], [13, 4, 4], [10, 4, 13]]

Your Answer
A)

[[0,1], [2,1], [3,1], [1,1], [2,0], [3,0], [0,2], [2,2], [3,2]]

Correct Answer
B)

[[0,1], [1,2], [2,1], [3,1], [1,1], [2,0], [3,0], [0,2], [2,2], [3,2]]

Explanation

It is possible for the water flow to reach both the Pacific and Atlantic Oceans from the cells at the following coordinates:

  • (0, 1)
  • (1, 2)
  • (2, 1)
  • (3, 1)
  • (1, 1)
  • (2, 0)
  • (3, 0)
  • (0, 2)
  • (2, 2)
  • (3, 2)
C)

[[0,1], [1,2], [3,1], [1,1], [2,0], [3,0], [0,2], [2,2], [3,2]]

D)

[[0,1], [1,2], [2,1], [3,1], [1,1], [2,0], [3,0], [2,2], [3,2]]

Question 3 of 33 attempted

Try it yourself#

Implement your solution in the following coding playground:

Note: The order in which the 2D grid appears doesn’t matter.

An optimal solution to this problem runs in O(m.n) time and takes O(m.n) space.

Python
usercode > main.py
Powered by AI
Input #1
Pacific Atlantic Water Flow

You might want to go over the Graphs pattern again.

Median of Two Sorted Arrays

Contains Duplicate